// 圣经问答游戏 - 音频系统

class AudioSystem {
    constructor() {
        this.audioContext = null;
        this.sounds = {};
        this.enabled = true;
        this.volume = 0.5;
        
        this.initAudioContext();
        this.createSounds();
    }

    // 初始化音频上下文
    initAudioContext() {
        try {
            // 创建音频上下文
            this.audioContext = new (window.AudioContext || window.webkitAudioContext)();
            
            // 处理浏览器的自动播放策略
            if (this.audioContext.state === 'suspended') {
                document.addEventListener('click', () => {
                    this.audioContext.resume();
                }, { once: true });
            }
        } catch (error) {
            console.warn('音频上下文创建失败:', error);
            this.audioContext = null;
        }
    }

    // 创建音效
    createSounds() {
        if (!this.audioContext) return;

        // 正确答案音效 - 愉快的和弦
        this.sounds.correct = () => {
            const frequencies = [523.25, 659.25, 783.99]; // C5, E5, G5
            this.playChord(frequencies, 0.3, 'sine');
        };

        // 错误答案音效 - 低沉的音调
        this.sounds.wrong = () => {
            const frequency = 220; // A3
            this.playTone(frequency, 0.4, 'sawtooth', 0.1);
        };

        // 成就音效已移除

        // 点击音效 - 短促的提示音
        this.sounds.click = () => {
            this.playTone(800, 0.1, 'sine', 0.05);
        };
    }

    // 播放单音
    playTone(frequency, duration, waveType = 'sine', volume = 0.1) {
        if (!this.audioContext || !this.enabled) return;

        try {
            const oscillator = this.audioContext.createOscillator();
            const gainNode = this.audioContext.createGain();

            oscillator.connect(gainNode);
            gainNode.connect(this.audioContext.destination);

            oscillator.frequency.setValueAtTime(frequency, this.audioContext.currentTime);
            oscillator.type = waveType;

            // 设置音量包络
            const now = this.audioContext.currentTime;
            gainNode.gain.setValueAtTime(0, now);
            gainNode.gain.linearRampToValueAtTime(volume * this.volume, now + 0.01);
            gainNode.gain.exponentialRampToValueAtTime(0.001, now + duration);

            oscillator.start(now);
            oscillator.stop(now + duration);
        } catch (error) {
            console.warn('播放音效失败:', error);
        }
    }

    // 播放和弦
    playChord(frequencies, duration, waveType = 'sine', volume = 0.08) {
        frequencies.forEach(freq => {
            this.playTone(freq, duration, waveType, volume);
        });
    }

    // 播放音效
    playSound(soundName) {
        if (this.sounds[soundName]) {
            this.sounds[soundName]();
        }
    }

    // 设置音量
    setVolume(volume) {
        this.volume = Math.max(0, Math.min(1, volume));
    }

    // 启用/禁用音效
    setEnabled(enabled) {
        this.enabled = enabled;
    }

    // 检查音频支持
    isSupported() {
        return !!this.audioContext;
    }
}

// 创建全局音频系统实例
window.audioSystem = new AudioSystem();